﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace NJGZBTYHXT
{
    /// <summary>
    /// 随机数组操作类
    /// </summary>
    public class RandomArray
    {
        #region

        private int _minValue;
        /// <summary>
        /// 最小值
        /// </summary>
        public int MinValue
        {
            get { return _minValue; }
            set { _minValue = value; }
        }

        private int _maxValue;
        /// <summary>
        /// 最大值
        /// </summary>
        public int MaxValue
        {
            get { return _maxValue; }
            set { _maxValue = value; }
        }

        private int _length;
        /// <summary>
        /// 数组长度
        /// </summary>
        public int Length
        {
            get { return _length; }
            set { _length = value; }
        }

        public RandomArray()
        {
            _minValue = 0;
            _maxValue = 9;
            _length = 10;
        }

        #endregion

        /// <summary>
        /// 随机数组，默认长度Length
        /// </summary>
        /// <returns></returns>
        public int[] Start()
        {
            try
            {
                Random rnd = new Random();
                int length = _maxValue - _minValue + 1;
                byte[] keys = new byte[length];
                rnd.NextBytes(keys);
                int[] items = new int[length];
                for (int i = 0; i < length; i++)
                {
                    items[i] = i + _minValue;
                }
                Array.Sort(keys, items);
                int[] result = new int[_length];
                Array.Copy(items, result, _length);
                return result;
            }
            catch
            {
                return new int[0];
            }
        }

        /// <summary>
        /// 随机指定长度的数组
        /// </summary>
        /// <returns></returns>
        public int[] Start(int arrayLength)
        {
            try
            {
                Random rnd = new Random();
                int length = _maxValue - _minValue + 1;
                byte[] keys = new byte[length];
                rnd.NextBytes(keys);
                int[] items = new int[length];
                for (int i = 0; i < length; i++)
                {
                    items[i] = i + _minValue;
                }
                Array.Sort(keys, items);
                int[] result = new int[arrayLength];
                Array.Copy(items, result, arrayLength);
                return result;
            }
            catch
            {
                return new int[0];
            }
        }

        /// <summary>
        /// 设置部分数组后随机余下的数组
        /// </summary>
        /// <param name="args">部分数组</param>
        /// <returns></returns>
        public int[] Start(int[] args)
        {
            int[] result = Start();

            if (args == null)
            {
                return result;
            }
            else
            {
                int length = args.Length;

                if (args.Length > result.Length)
                {
                    length = result.Length;
                }
                for (int i = 0; i < length; i++)
                {
                    if (!IsContains(result, args[i]))
                    {
                        result[i] = args[i];
                    }
                }
                return result;
            }
        }

        public int[] StartByList(int[] args)
        {
            _maxValue = args.Length;

            if (_maxValue <= _length)
            {
                return args;
            }

            int[] result = Start();

            int[] newResult = new int[_length];

            for (int i = 0; i < result.Length; i++)
            {
                newResult[i] = args[result[i]];
            }

            return newResult;
        }

        public int[] StartByList(int[] args, int[] args2)
        {
            _maxValue = args2.Length - 1;

            int[] result = Start();

            int[] newResult = new int[_length];

            for (int i = 0; i < result.Length; i++)
            {
                newResult[i] = result[i];
            }

            for (int i = 0; i < args.Length; i++)
            {
                if (IsContains(args2, args[i]))
                {
                    if (!IsContains(newResult, args[i]))
                    {
                        newResult[i] = args[i];
                    }
                }
            }

            return newResult;
        }

        public int[] Start(int count, int[] args)
        {
            _length = count;
            return Start(args);
        }

        /// <summary>
        /// 数组乱序
        /// </summary>
        /// <param name="jg">预排序数组</param>
        /// <returns></returns>
        public static int[] SortByJumble(int[] jg)
        {
            int[] arra = new int[jg.Length];

            for (int i = 0; i < arra.Length; i++)
            {
                arra[i] = jg[i];
            }

            for (int i = arra.Length - 1; i > 0; i--)
            {
                Random rand = new Random();
                int p = rand.Next(i);
                int temp = arra[p];
                arra[p] = arra[i];
                arra[i] = temp;
            }
            return arra;
        }

        /// <summary>
        /// 判断数组中是否存在某个数值
        /// </summary>
        /// <param name="result">数组</param>
        /// <param name="p">数值</param>
        /// <returns></returns>
        private bool IsContains(int[] result, int p)
        {
            for (int j = 0; j < result.Length; j++)
            {
                if (p == result[j])
                {
                    return true;
                }
            }
            return false;
        }
    }

    public class RandomNumber
    {
        public int RandomNum(int min, int max)
        {
            Random r = new Random();
            return r.Next(min, max);
        }
    }
}
